package phoneregistration.controller.converter.impl;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import phoneregistration.controller.converter.UserConverter;
import phoneregistration.controller.model.UserModel;
import phoneregistration.domain.User;

@Service
public class UserConverterImpl implements UserConverter {

    public UserModel convertEntityToModel(User e) {
        assertEntity(e);

        UserModel model = new UserModel();
        model.setId(e.getId() + "");
        model.setName(e.getName());

        return model;
    }

    public User convertModelToEntity(UserModel e) {
        assertModel(e);

        User entity = new User();

        String idString = e.getId();
        if (idString != null) {
            long id = Long.parseLong(idString);
            entity.setId(id);
        }

        entity.setName(e.getName());

        return entity;
    }

    private void assertEntity(User e) {
        Assert.notNull(e);
        Assert.notNull(e.getId());
        Assert.notNull(e.getName());
    }

    private void assertModel(UserModel e) {
        Assert.notNull(e);
        Assert.notNull(e.getName());
    }
}
